package com.android.nearshop.util;

import android.content.Context;
import android.location.Location;
import android.net.ConnectivityManager;
import android.os.Environment;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import com.android.nearshop.R;
import com.android.nearshop.app.MyApplication;
import com.android.nearshop.app.widget.TipText;
import com.android.nearshop.business.cache.DataCacheManager;
import com.android.nearshop.business.location.MyBMapManager;
import com.baidu.mapapi.GeoPoint;

/*********************************************************
 * @author : zhaohua
 * @version : 2012-8-8
 * @see : 通用不好分类的工具类
 * @Copyright : copyrights reserved by personal 2007-2011
 **********************************************************/
public class Util
{

    public static boolean isEmpty(String str)
    {
      return ((str == null) || (str.length() == 0) || (str.equals("null")));
    }
    
    public static void showToast(Context paramContext, int paramInt)
    {
      TipText.createTipText(paramContext, paramContext.getResources().getString(paramInt), 0).show();
    }
    
    public static void showToast(Context paramContext, String paramString)
    {
      TipText.createTipText(paramContext, paramString, 0).show();
    }

    public static GeoPoint locationToGeoPoint(Location paramLocation)
    {
      if (paramLocation == null) return null;
      return new GeoPoint((int)(1000000.0D * paramLocation.getLatitude()), (int)(1000000.0D * paramLocation.getLongitude()));
    }
    
    public static GeoPoint toGeoPoint(double lon, double lat)
    {
      return new GeoPoint((int)(1000000.0D * lat), (int)(1000000.0D * lon));
    }

    public static String getExternDir(String dir)
    {
        String path = Environment.getExternalStorageDirectory().getAbsolutePath();
        path += dir;
        return path;
    }
    
    /**
     * 当前位置镇区
     */
    public static String getCityText(Context context)
    {
        int mLocMode = MyApplication.getContext().getLocMode();
        String cityName = context.getString(R.string.loc_default);
        
        switch(mLocMode)
        {
            case Constant.LOC_CUR:
            {
                if(MyBMapManager.getInstace(context).getMyAddrInfo() != null)
                {
                    cityName = MyBMapManager.getInstace(context).getMyAddrInfo().addressComponents.district;
                }
                break;
            }
            case Constant.LOC_MAP:
            {
                String city = MyApplication.getContext().getLocMapDistrict();
                if(!TextUtils.isEmpty(city))
                {
                    cityName = city;
                }
                break;
            }
            case Constant.LOC_CITY:
            {
                int areaId = MyApplication.getContext().getLocCityValue();
                String city = DataCacheManager.getInstance().getAreaNameById(areaId);
                if(!TextUtils.isEmpty(city))
                {
                    cityName = city;
                }
                break;
            }
        }
        
        return cityName;
    }
    
    public static boolean isConnectionFast(int type, int subType)
    {
        if (type == ConnectivityManager.TYPE_WIFI)
        {
            return true;
        } else if (type == ConnectivityManager.TYPE_MOBILE)
        {
            switch (subType)
            {
            case TelephonyManager.NETWORK_TYPE_1xRTT:
                return false; // ~ 50-100 kbps
            case TelephonyManager.NETWORK_TYPE_CDMA:
                return false; // ~ 14-64 kbps
            case TelephonyManager.NETWORK_TYPE_EDGE:
                return false; // ~ 50-100 kbps
            case TelephonyManager.NETWORK_TYPE_EVDO_0:
                return true;  // ~ 400-1000 kbps
            case TelephonyManager.NETWORK_TYPE_EVDO_A:
                return true;  // ~ 600-1400 kbps
            case TelephonyManager.NETWORK_TYPE_GPRS:
                return false; // ~ 100 kbps
            case TelephonyManager.NETWORK_TYPE_HSDPA:
                 return true; // ~ 2-14 Mbps
            case TelephonyManager.NETWORK_TYPE_HSPA:
                 return true; // ~ 700-1700 kbps
            case TelephonyManager.NETWORK_TYPE_HSUPA:
                 return true; // ~ 1-23 Mbps
            case TelephonyManager.NETWORK_TYPE_UMTS:
                return true;  // ~ 400-7000 kbps
            case TelephonyManager.NETWORK_TYPE_IDEN:
                return false; // ~25 kbps
            case TelephonyManager.NETWORK_TYPE_UNKNOWN:
                return false;
            default:
                return false;
            }
        } else
        {
            return false;
        }
    }
}
